<?php

include "Meeting.php";
include "UserMeeting.php";

class MeetingDAO extends DataLayer {	
	/**
	* @desc Construtor da Classe
 	*/
	function __construct($lk){
		$this->lk = $lk;	
	}

/******************************************************************************************************************
Funções das Reuniões
******************************************************************************************************************/
	public function getMeetingTypes($id=0){
		$this->sql ="	SELECT 
							id_reuniao_tipo id, nome name, descricao description
						FROM 
							reunioes_tipos";
		if($id > 0)
			$this->sql .= " WHERE id_reuniao_tipo = ".$id;

		$this->sql .= " ORDER BY id_reuniao_tipo";
				
		return $this->result = parent::select($this->sql);
	}

	public function getMeetings($idMeeting=0,$idSprint=0,$page=0){
		$this->sql ="	SELECT 
							SQL_CALC_FOUND_ROWS
							r.id_reuniao, r.id_sprint, r.sintese, r.descricao, DATE_FORMAT(r.data_inicial, '%d/%m/%Y - %H:%i') data_inicial, DATE_FORMAT(r.data_final, '%d/%m/%Y - %H:%i') data_final, DATE_FORMAT(r.data, '%d/%m/%Y') data,	rt.id_reuniao_tipo idMeetingType, rt.nome meetingType
						FROM 
							reunioes r
						INNER JOIN
							reunioes_tipos rt on rt.id_reuniao_tipo = r.id_reuniao_tipo
						INNER JOIN
							sprints s on s.id_sprint = r.id_sprint
						WHERE
							r.status = 1";

		if($idSprint > 0)						
			$this->sql .= "  AND s.id_sprint = ".$idSprint;

		if($idMeeting > 0)
			$this->sql .= "  AND r.id_reuniao = ".$idMeeting;

		$this->sql .= " ORDER BY r.id_reuniao DESC";

		if($page > 0){
			$page = (int)$page;
			$page--;
			if($page < 0)
				$page = 0;
			else
				$page *= PAGINATION;

			$this->sql .= " LIMIT ".$page.",".PAGINATION;
		}

		$this->result = parent::select($this->sql);
		$meetingList = NULL;
		
		if(count($this->result) > 0){
			$i = 0;
			foreach($this->result as $row){
				$meetingList[$i] = new Meeting();				
				$meetingList[$i]->setDadosDB($row);				

				$i++;
			}
		}
		
		return $meetingList;
	}	

	public function save(Meeting $obj){
		if($obj != NULL){
			$dbObj = $obj->fieldsDBObj;
			unset($dbObj["id_reuniao"]);
			if($obj->id > 0){
				parent::update("reunioes",$dbObj,"id_reuniao = ".$obj->id);
				return $obj->id;
			}else{
				return parent::insert("reunioes",$dbObj,"data");
			}
		}else{
			return 0;
		}
	}

	public function deleteMeeting($id=0){
		if($id > 0){
			$rsMeeting = $this->getMeetings($id);

			if(count($rsMeeting) > 0){
				if($rsMeeting[0]->meetingType == "Acompanhamento"){
					return -1;
				}else{
					$dados["status"] = 0;
					return parent::update("reunioes", $dados, "id_reuniao=".$id);
				}				
			}
		}

		return 0;
	}

/******************************************************************************************************************
Funções do Calendario
******************************************************************************************************************/
	public function getMeetingsCalendar($idProjeto){
		$this->sql ="	SELECT 
							SQL_CALC_FOUND_ROWS
							r.id_reuniao, r.id_sprint, r.sintese, r.descricao, r.data_inicial, r.data_final, r.data, rt.id_reuniao_tipo idMeetingType, rt.nome meetingType, u.nome nameCriator
						FROM 
							reunioes r
						INNER JOIN
							reunioes_tipos rt on rt.id_reuniao_tipo = r.id_reuniao_tipo
						INNER JOIN
							sprints s on s.id_sprint = r.id_sprint
						INNER JOIN
							usuarios u on u.id_usuario = r.id_usuario_criador
						WHERE
							r.status = 1";

		if($idProjeto > 0)						
			$this->sql .= "  AND s.id_projeto = ".$idProjeto;

		if($page > 0){
			$page = (int)$page;
			$page--;
			if($page < 0)
				$page = 0;
			else
				$page *= PAGINATION;

			$this->sql .= " LIMIT ".$page.",".PAGINATION;
		}

		$this->result = parent::select($this->sql);
		$meetingList = NULL;
		
		if(count($this->result) > 0){
			$i = 0;
			foreach($this->result as $row){
				$meetingList[$i] = new Meeting();				
				$meetingList[$i]->setDadosDB($row);				

				$i++;
			}
		}
		
		return $meetingList;
	}

/******************************************************************************************************************
Funções dos Sprints para as reuniões
******************************************************************************************************************/

	public function getSprints($id=0, $idProject){
	
		$this->sql ="	SELECT
							s.id_sprint, s.id_equipe, s.id_projeto, s.ordem,
							DATE_FORMAT(s.data_inicial, '%d/%m/%Y') data_inicial,
							(SELECT COUNT(id_reuniao) FROM reunioes WHERE status <> 0 and id_sprint = s.id_sprint) meetings
						FROM 
							sprints s
						WHERE 
							s.status <> 0 and s.id_projeto = ".$idProject;

		if($id > 0)						
			$this->sql .= " and s.id_sprint = ".$id;
		
		$this->sql .= " ORDER BY s.ordem DESC";
		
		$this->result = parent::select($this->sql);
		$sprintsList = NULL;
		
		if(count($this->result) > 0){
			$i = 0;
			foreach($this->result as $row){
				$sprintsList[$i] = new Sprint();
				$sprintsList[$i]->setDadosDB($row);

				$i++;
			}
		}
		
		return $sprintsList;
	}


/******************************************************************************************************************
Funções de Detalhamento por Usuário
******************************************************************************************************************/
	public function getUserMeeting($idMeeting=0,$idUser=0){
		$this->sql ="	SELECT 
							ru.id_reuniao_usuario, ru.id_reuniao, ru.id_usuario, ru.o_que_fez, ru.o_que_esta_fazendo, ru.impedimentos, ru.descricao, DATE_FORMAT(ru.data, '%d/%m/%Y') data 
						FROM 
							reunioes_usuarios ru
						INNER JOIN
							reunioes r on r.id_reuniao = ru.id_reuniao
						WHERE
							r.status = 1";

		if($idMeeting > 0)
			$this->sql .= "  AND r.id_reuniao = ".$idMeeting;

		if($idUser > 0)						
			$this->sql .= "  AND ru.id_usuario = ".$idUser;

		$this->sql;

		$this->result = parent::select($this->sql);
		$meetingList = NULL;
		
		if(count($this->result) > 0){
			$i = 0;
			foreach($this->result as $row){
				$meetingList[$i] = new UserMeeting();				
				$meetingList[$i]->setDadosDB($row);				

				$i++;
			}
		}
		
		return $meetingList;
	}

	public function saveUserMeeting(UserMeeting $obj){
		if($obj != NULL){
			$dbObj = $obj->fieldsDBObj;
			unset($dbObj["id_reuniao_usuario"]);
			if($obj->id > 0){
				parent::update("reunioes_usuarios",$dbObj,"id_reuniao_usuario = ".$obj->id,"data");
				return $obj->id;
			}else{
				return parent::insert("reunioes_usuarios",$dbObj,"data");
			}
		}else{
			return 0;
		}
	}
}